#===============================================================================
# Koro Business Environment
# Copyright (C) 2008  Antonio Corroppoli
#
#This file is part of Koro BE.
# 
# Koro BE is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Koro BE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Koro BE; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#===============================================================================

'''
Created on 11/ago/2011

@author: antstud
'''

from koro.db import database
from koro.core.context import Context
from koro.modules.anagrafiche import model

#===============================================================================
# ClientiContext
#===============================================================================
class ClientiContext(Context):
    def __init__(self):
        Context.__init__(self,model.ClienteFornitore)

    def delete_object(self, obj):
        if obj.ID==1:
            return
        return Context.delete_object(self, obj)

#    def new_object(self):
#        new_obj=model.ClienteFornitore()
#        new_obj.tipo='C'
#        #new_obj.fk_listino=self._get_options().LISTINO_DEFAULT_VENDITA
#        #new_obj.fk_iva=self._get_options().IVA_DEFAULT
#        database.get_instance().save(new_obj)
#        return new_obj
    
    def _configure_new_object(self,obj):
        obj.tipo='C'
        obj.fk_listino=self._get_options().LISTINO_DEFAULT_VENDITA
    
    def _configure_query(self,query):
        return query.filter_by(tipo=u'C').filter(model.ClienteFornitore.ID!=1)
        
#    def get_objects(self,*args,**kwargs):
#        query=database.get_instance().query(self._context)
#        query=query.filter_by(tipo=u'C').filter(model.ClienteFornitore.ID>1)
#        if len(kwargs)>0:
#            query=query.filter_by(**kwargs)
#        if len(args)>0:
#            for a in args:
#                query=query.filter(a)
#        return query.all()        

#    def save_object(self,obj):
#        obj.tipo='C'
#        if obj.fk_listino is None:
#            obj.fk_listino=self._get_options().LISTINO_DEFAULT_VENDITA
##        if obj.fk_iva is None:
##            obj.fk_iva=self._get_options().IVA_DEFAULT
##        database.get_instance().validate_and_save(obj)
#        Context.save_object(self, obj)
    
    


#===============================================================================
# FornitoriContext
#===============================================================================
class FornitoriContext(Context):
    def __init__(self):
        Context.__init__(self,model.ClienteFornitore)
    
    def delete_object(self, obj):
        if obj.ID==2:
            return
        return Context.delete_object(self, obj)
    
#    def new_object(self):
#        new_obj=model.ClienteFornitore()
#        new_obj.tipo='F'
#        new_obj.fk_listino=self._get_options().LISTINO_DEFAULT_ACQUISTO
#        new_obj.fk_iva=self._get_options().IVA_DEFAULT
#        database.get_instance().save(new_obj)
#        return new_obj
    
    def _configure_new_object(self,obj):
        obj.tipo='F'
        obj.fk_listino=1
    
#    def get_objects(self,*args,**kwargs):
#        query=database.get_instance().query(self._context).filter_by(tipo=u'F')
#        if len(kwargs)>0:
#            query=query.filter_by(**kwargs)
#        if len(args)>0:
#            for a in args:
#                query=query.filter(a)
#        return query.all()
    
    def _configure_query(self,query):
        return query.filter_by(tipo=u'F').filter(model.ClienteFornitore.ID!=2)

#    def save_object(self,obj):
#        obj.tipo='F'
#        if obj.fk_listino is None:
#            obj.fk_listino=1#il listino fornitore ha codice 1
#        #database.get_instance().validate_and_save(obj)
#        Context.save_object(self, obj)


#===============================================================================
# AziendaContext
#===============================================================================
class AziendaContext(Context):
    def __init__(self):
        Context.__init__(self,model.Azienda)
    
    def get_dati_azienda(self,*args,**kwargs):
        query=database.get_instance().query(self._context)
        azienda=query.first()
        if azienda is None:
            azienda=model.Azienda()
        return azienda
    
    def save_dati_azienda(self,*args,**kwargs):
        database.get_instance().save(args[0])